// pages/settings/settings.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    uid: 0,
    openid: null
  },
  goNewVerison: function (e) {
    var that = this
    console.log(e)
    var type = e.currentTarget.dataset.type
    if (type == 0) //没有订阅过
    {
      that.dingyue()

    } else {
      that.changeDingyue()
    }


  },
  dingyue: function (e) {
    var that = this
    //订阅消息模板id
    wx.requestSubscribeMessage({
      tmplIds: ['QaxL2tUtgefFkkyix1OgYorDOYdWbyGWCmOWKDAxbzQ'],
      success(res) {

        //发送access_token求
        wx.request({
          url: app.data.service_url + '/sen/sendMessage',
          data: {
            //数据包
            data: {
              //openid
              "touser": that.data.openid,
              //模板id
              "template_id": "QaxL2tUtgefFkkyix1OgYorDOYdWbyGWCmOWKDAxbzQ",
              "page": "index",
              "miniprogram_state": "developer",
              "lang": "zh_CN",
              "data": {
                "thing1": { //必须和模板的保持一致
                  "value": "参与游戏订阅提醒"
                },
                "thing2": {  //必须和模板的保持一致
                  "value": "超级好玩的游戏等你来体验哦！"
                }

              }
            }
          },
          method: "GET",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"

          },
          success: function (res) {
            console.log("订阅成功");
            console.log(res);
            that.changeDingyue()
          },
          fail: function (res) {
            console.log("订阅失败");
            console.log(res);
          },
        })
      }
    })




  },

  changeDingyue: function () {
    var that = this
    wx.request({
      url: app.data.service_url + 'User/updUserLog_Tixing',
      data: {
        uid: that.data.uid
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        console.log(res)
        wx.switchTab({
          url: '/pages/tabs/profile/profile',
        })
      }
    })

  },

  goAboutUs: function () {
    var that = this
    wx.request({
      url: app.data.service_url + 'User/updUserLog_Yinxiao',
      data: {
        uid: that.data.uid
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        console.log(res)
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getUserInfo: function (uid) {

    //获取个人信息
    var that = this
    wx.request({
      url: app.data.service_url + 'Login/getUserInfo',
      data: {
        uid: uid
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        console.log("---------------------------------------")
        console.log(res)
        var userInfo = res.data.data[0]
        if (userInfo.openid == "" || userInfo.is_login == 0) {
          wx.showToast({
            title: '请先授权登录',
            icon: "none",
            success(res) {
              wx.reLaunch({

                url: '/pages/login/login',
              })
            }
          })
          return false
        }

        that.setData({
          userInfo: userInfo,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.getStorage({
      key: 'uid',
      success(res) {
        that.setData({
          uid: res.data
        })
        that.getUserInfo(res.data)
      }
    })
    wx.getStorage({
      key: 'openid',
      success(res) {
        that.setData({
          openid: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})